import { defineStore } from "pinia";
import {getCurrentUser, logout as logoutApi, User} from "@/api/sysController.ts";
import router from "@/router";

export const userStore = defineStore("user", {
    state: () => {
        return {
            userInfo: null as User | null,
            rememberMe: {
                username: '',
                password: ''
            },
            phoneMenuShow: false
        }
    },
    getters: {

    },
    actions: {
        setPhoneMenuShow(show: boolean) {
            this.phoneMenuShow = show
        },
        setUserInfo(userInfo: User) {
            this.userInfo = userInfo
        },
        setRememberMe(username: string, password: string) {
            console.log('记住我', username, password)
            this.rememberMe.username = username
            this.rememberMe.password = password
        },
        logout() {
            this.userInfo = null
            router.push('/login')
            logoutApi()
        },
        setCurrentUser() {
            getCurrentUser().then(res => {
                this.userInfo = res as User
            })
        }
    },
    persist: true
})